import moment from 'moment';
import { useEffect } from 'react';
import { IApplyActionEnum } from '../common';

/**
 * 处理JSON中日期格式
 * @param timeList
 * @param timeName
 * @returns
 */
const handListMoment = (timeList: Record<string, any>[], timeName: string[], momentType: boolean) => {
  let momentList: Record<string, any>[] = [];
  timeList?.forEach((itemData: any) => {
    let itemObj: Record<string, any> = { ...itemData };
    timeName?.forEach((itemCode: any) => {
      if (itemCode && itemObj?.[itemCode]) {
        if (momentType) {
          itemObj[itemCode] = moment(itemObj?.[itemCode])?.format('YYYY-MM-DD HH:mm:ss');
        } else {
          itemObj[itemCode] = moment(itemObj?.[itemCode]);
        }
      }
    });
    momentList?.push(itemObj);
  });

  return momentList;
};

/** OA详情监听渲染 */
const applyInfoEffect = (detailInfo: Record<string, any>, applyForm: any) => {
  /** 初始化数据 */
  useEffect(() => {
    if (detailInfo && detailInfo?.id) {
      const newDetailInfo: Record<string, any> = {
        ...detailInfo,
        applyTime: detailInfo?.applyTime?.split(' ')?.[0],
        applyTitle: detailInfo?.applyTitle,
        applyCode: detailInfo?.applyCode,
        applyDepartmentName: detailInfo?.applyDepartmentName,
        applyerName: detailInfo?.applyerName,
        supplyKind: detailInfo?.supplyKind,
        contactMobile: detailInfo?.contactMobile,
        expectSum: detailInfo?.expectSum,
        applyInfo: detailInfo?.applyInfo,
      };
      applyForm?.setFieldsValue({
        ...newDetailInfo,
      });
    }
  }, [detailInfo?.id]);
};

export { handListMoment, applyInfoEffect };
